//
//  EditPhonePollingViewController.m
//  OperationPro
//
//  Created by yaxue wang on 2017/1/6.
//  Copyright © 2017年 展鸿科技. All rights reserved.
//

#import "EditPhonePollingViewController.h"
#import "ConfirmPhoneView.h"
#import "CreateProduceViewController.h"
#import "EditProduceViewController.h"
#import "PollingDetailViewController.h"
@interface EditPhonePollingViewController ()<UITextFieldDelegate>
@property(nonatomic,strong)CTextField *phoneText;
@property(nonatomic,strong)CTextField *nameText;
@end

@implementation EditPhonePollingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"修改联系人"];

   UIView *bgView= [[UIView alloc] initWithFrame:CGRectMake(0, 74, WindowWidth, WindowHeight-74)];
    bgView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:bgView];

    _phoneText =[[CTextField alloc]initWithFrame:CGRectMake(15, 0, WindowWidth-30, 50)];
    _phoneText.placeholder =@"联系人手机号";
    [_phoneText setNullMsg:@"请输入11位手机号!"];
    _phoneText.text=self.phoneString;
    _phoneText.delegate=self;
    _phoneText.font =F(15);
    [_phoneText setInputType:CTextFieldInputTypePhone];
    _phoneText.tag =100;
    [bgView addSubview:_phoneText];
    UILabel *line1=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_phoneText.frame), WindowWidth-30, 0.5)];
    line1.backgroundColor=LineColor;
    [bgView addSubview:line1];
    
    _nameText =[[CTextField alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(line1.frame), WindowWidth-30, 50)];
    _nameText.placeholder =@"联系人姓名";
    _nameText.text=self.name;
    [_nameText setBk_shouldChangeCharactersInRangeWithReplacementStringBlock:^BOOL(UITextField *tfd, NSRange range, NSString *str) {
        if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
            return NO;
        }
        
        return YES;
    }];
    [_nameText bk_addEventHandler:^(id sender) {
        
        UITextPosition *position = [_nameText positionFromPosition:[_nameText markedTextRange] offset:0];
        if (position) {
            return;
        }
        NSInteger maxLength = 20;
        if (maxLength > 0) {
            NSInteger splitcount;
            if (splitcount > 0) {
                maxLength = maxLength + maxLength /splitcount - (maxLength % splitcount == 0 ? 1 : 0);
            }
            if (_nameText.text.length > maxLength) {
                _nameText.text = [_nameText.text substringWithRange:NSMakeRange(0, maxLength)];
            }
            //移除表情
            NSString *newStr= [CClass disable_emoji:_nameText.text];
            _nameText.text=newStr;
            
            
            
            
        }
        
    } forControlEvents:UIControlEventEditingChanged];
    _nameText.font =F(15);
    [_nameText setInputType:CTextFieldInputTypeDefault];
    [bgView addSubview:_nameText];
    UILabel *line2=[[UILabel alloc]initWithFrame:CGRectMake(15, CGRectGetMaxY(_nameText.frame), WindowWidth-30, 0.5)];
    line2.backgroundColor=LineColor;
    [bgView addSubview:line2];
    CGFloat width=(WindowWidth-20-30)/2;
    UIButton*confirmBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:confirmBtn];
    confirmBtn.frame=CGRectMake(15, CGRectGetMaxY(line2.frame)+40, width, 40);
    [confirmBtn setBackgroundColor:MainColor];
    confirmBtn.layer.masksToBounds=YES;
    confirmBtn.layer.cornerRadius=8;
    [confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    [confirmBtn bk_addEventHandler:^(id sender) {
        if (!_phoneText.verfiy) {
            
            return;
        }
        if (_nameText.text.length<=0) {
            [CClass showMessage:@"请输入联系人姓名" completion:^{
                
            }];
            return;
        }
        if([_phoneText.value isEqualToString:@""])
        {
            [CClass showMessage:@"不能添加相同的联系人！"];
            return;
        }
        [self.view endEditing:YES];
        NSMutableDictionary *dataDic=[[NSMutableDictionary alloc]init];
        [dataDic setObject:_phoneText.value forKey:@"mobile"];
        [dataDic setObject:_nameText.text forKey:@"name"];
        [self.mobileAr replaceObjectAtIndex:self.index withObject:dataDic];

        EditProduceViewController *editVc=[[EditProduceViewController alloc]init];

        editVc.Uuid=self.Uuid;
        editVc.mobileAr=self.mobileAr;
        editVc.phoneString=_phoneText.text;
        editVc.isHasPhone=self.isHasPhone;
        [self pushController:editVc];

    } forControlEvents:UIControlEventTouchUpInside];
    
    UIButton*delectBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    [bgView addSubview:delectBtn];
    delectBtn.frame=CGRectMake(20+CGRectGetMaxX(confirmBtn.frame), CGRectGetMaxY(line2.frame)+40, width, 40);
    [delectBtn setBackgroundColor:[UIColor whiteColor]];
    delectBtn.layer.masksToBounds=YES;
    delectBtn.layer.cornerRadius=8;
    delectBtn.layer.borderColor=MainColor.CGColor;
    delectBtn.layer.borderWidth=1;
    [delectBtn setTitle:@"删除联系人" forState:UIControlStateNormal];
    [delectBtn setTitleColor:MainColor forState:UIControlStateNormal];
    [delectBtn bk_addEventHandler:^(id sender) {
        [self.view endEditing:YES];
//        @property(nonatomic,strong)NSString *isHasPhone;//是否有联系人 0是没有 1是有
        [self.mobileAr removeObjectAtIndex:self.index];

        if ([_isHasPhone isEqualToString:@"0"]) {
            CreateProduceViewController *produceVC=[[CreateProduceViewController alloc]init];
            produceVC.Uuid=self.Uuid;
            produceVC.mobileAr=self.mobileAr;
            [self pushController:produceVC];
        }else
        {
            CreateProduceViewController *produceVC=[[CreateProduceViewController alloc]init];
            produceVC.Uuid=self.Uuid;
            produceVC.mobileAr=self.mobileAr;

            [self pushController:produceVC];
        }
        
       


    } forControlEvents:UIControlEventTouchUpInside];
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *shouldBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (textField==_phoneText) {
        if (shouldBeString.length==13) {
            NSString *v = shouldBeString;
            
            v = [shouldBeString stringByReplacingOccurrencesOfString:@" " withString:@""];
            //通过电话发送请求找到姓名 还没有判断注册没有
            [CClass HttpRequestTask:YES withUrl:@"customer_findUser" withData:@{@"mobile":v} notifyError:YES onSuccess:^(BaseModel *model) {
                if ([[model.B_data allKeys] containsObject:@"name"]) {
                    _nameText.userInteractionEnabled=NO;
                    _nameText.text=[model.B_data objectForKey:@"name"];
                    [self.view endEditing:YES];
                    
                }else
                {
                    _nameText.text=@"";
                    _nameText.userInteractionEnabled=YES;
                    
                }
                
                
            } onError:^(BaseModel *model) {
                _nameText.text=@"";
                _nameText.userInteractionEnabled=YES;
            }];
            
        }else
        {
            _nameText.text=@"";
            _nameText.userInteractionEnabled=YES;
        }
    }else
    {
        if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
            return NO;
        }
    }
    
    
    return YES;
    
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

@end
